﻿// ***********************************************************************
// Assembly         : OSS.BIM
// Author           : 田腾
// Created          : 05-16-2018
//
// Last Modified By : 田腾
// Last Modified On : 07-12-2018
// ***********************************************************************
// <copyright file="GroupModelEditCmd.cs" company="北京智帆高科科技有限公司">
//     Copyright ©  2017
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Windows.Forms;
using OSS.BIM.Business;
using OSS.BIM.Model;
using OSS.BIM.SceneManage;
using OSS.BIM.UI.PropertyGrids;
using OSS.BIM.Utility;
using ZFGK.Addins;
using ZFGK.Delegates;
using ZFGK.Utility;
using ZFGK.DevExprss.Forms;
using ZFGK.osgEarth.UI;

namespace OSS.BIM.Commands.GroupModelCmds
{
    /// <summary>
    /// Class GroupModelEditCmd.
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    public class GroupModelEditCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
//             if (!PermissionApp.CurUser.CheckUser())
//             {
//                 return CmdResult.Cancel;
//             }
            var form = cmdData.ViewForm as OsgEarthViewForm;
            var osgEarthViewCtrl = form.OsgEarthViewCtrl;
            ModelElement userModel = cmdData.Tag as ModelElement;
            if (userModel == null)
            {
                var actionUtility = osgEarthViewCtrl.ActionUtility;
                if (!actionUtility.PickUserModel(out userModel))
                {
                    return CmdResult.Cancel;
                }
            }
            if (userModel == null)
            {
                return CmdResult.Cancel;
            }
            userModel = userModel.DeepClone();
            ModelInfoBinding userModelBinding = new ModelInfoBinding(userModel);
            var propertyGridAddEditForm = new DvPropertyGridEditForm("编辑模型", userModelBinding);
            propertyGridAddEditForm.Icon = form.Icon;
            propertyGridAddEditForm.ValueValidated += delegate(object sender, ValueValidatedArgs args)
            {
                userModelBinding.Name = userModelBinding.Name.Trim();
                if (string.IsNullOrEmpty(userModelBinding.Name))
                {
                    args.ErrorMessage.AppendLine("模型名称不能为空!");
                    return false;
                }
                if (userModelBinding.Scale.x <= 0 || userModelBinding.Scale.y <= 0 || userModelBinding.Scale.z <= 0)
                {
                    args.ErrorMessage.AppendLine("缩放必须为正数!");
                    return false;
                }
                return true;
            };
            if (propertyGridAddEditForm.ShowDialog(cmdData.ViewForm as OsgEarthViewForm) != DialogResult.OK)
                return CmdResult.Cancel;

            if (userModel.IsModify() && DB.Context.Update<ModelElement>(userModel) == 0)
            {
                ZfMessageUtil.ShowError("更新数据库失败!", "数据库错误");
                return CmdResult.Cancel;
            }
            userModel.AddOrUpdateTreeListNode();
            osgEarthViewCtrl.OsgEarthObj.AddOrReplaceModel(userModel);
            return CmdResult.Succeed;
        }
    }
}
